package com.yksj.healthtalk.ui.recharge;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.ui.R;
/**
 * 日期选择主页
 * @author root
 *
 */
public class ChoiseDateActivity extends BaseFragmentActivity implements OnItemClickListener, OnClickListener {

	private TextView mNowyear;
	private String getTime;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getTime = getIntent().getStringExtra("date");
		setContentView(R.layout.choise_date_layout);
		initTitle();
		GridView grivd=(GridView)findViewById(R.id.grivd);
		grivd.setAdapter(new MyAdapter(this,getTime));
		grivd.setOnItemClickListener(this);
		initView();
	}

	private void initView() {
		titleLeftBtn.setOnClickListener(this);
		titleRightBtn2.setOnClickListener(this);
		titleRightBtn2.setText("全部");
		titleRightBtn2.setVisibility(View.VISIBLE);
		
		mNowyear = (TextView) findViewById(R.id.changge_year);
		mNowyear.setText(getTime.substring(0, 4));
		findViewById(R.id.calender_left).setOnClickListener(this);
		findViewById(R.id.calender_right).setOnClickListener(this);
		titleTextV.setText("请选择日期");
		titleTextV.setTextColor(Color.BLACK);
	}

	class MyAdapter extends BaseAdapter{
		private LayoutInflater mInflater;
		private List<String> list;
		private String dateNO;//目前选择的时间
		private String yue;
		
		public MyAdapter(Context context,String date) {
			dateNO=date;
			mInflater=LayoutInflater.from(context);
			list=new ArrayList<String>();
			for(int x=0;x<14;x++){
				if(x==0){
					list.add("");
				}else if(x<=12){
					list.add(x+"");
				}else{
					list.add("");
				}
			}
			Calendar cal=Calendar.getInstance();    
			int m=cal.get(Calendar.MONTH)+1; 
			yue=m+"";
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			convertView=(View)mInflater.inflate(R.layout.date_text_itemer,null);
			String substringText = dateNO.substring(5,dateNO.length());
			if(substringText.substring(0).startsWith("0")){
				substringText=substringText.substring(1);
			}
			if(list.get(position).equals(substringText)){
				convertView.setBackgroundResource(R.drawable.rili_bluecolor);	
			}
			if(yue.equals(list.get(position))){
				convertView.setBackgroundResource(R.drawable.rili_bluecolor1);	
			}
			TextView view =(TextView) convertView.findViewById(R.id.textview);
			view.setText(list.get(position));
			return convertView;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		String date="";
		if(position==0||position==13)return;
		if(position>0&&position<13){
			if(position<10){
				date = "0"+position;
			}else{
				date=position+"";
			}
		}
		Intent intent = ChoiseDateActivity.this.getIntent();
		Bundle bundle=new Bundle();
		bundle.putString("date", date);
		bundle.putString("year", mNowyear.getText().toString());
		intent.putExtras(bundle);
		setResult(RESULT_OK, intent);
		finish();
	}

	@Override
	public void onClick(View v) {
		int data = 0;
		Intent intent;
		Bundle bundle;
		switch (v.getId()) {
		case R.id.calender_left:
			data=Integer.parseInt(mNowyear.getText().toString().substring(0, 4));
			if(data==2013){
				return;
			}else{
				data--;
			}
			break;
		case R.id.calender_right:
			data=Integer.parseInt(mNowyear.getText().toString().substring(0, 4));
			data++;
			break;
		case R.id.title_right2://全部
			intent = getIntent();
			bundle=new Bundle();
			bundle.putString("all","all");
			intent.putExtras(bundle);
			setResult(RESULT_OK, intent);
			ChoiseDateActivity.this.finish();
		/*	ChoiseDateActivity.this.finish();
			overridePendingTransition(R.anim.back_enter, R.anim.back_exit);*/
			break;
		case R.id.title_back://全部
			intent = getIntent();
			bundle=new Bundle();
			bundle.putString("nothing","nothing");
			intent.putExtras(bundle);
			setResult(RESULT_OK, intent);
			ChoiseDateActivity.this.finish();
			overridePendingTransition(R.anim.back_enter, R.anim.back_exit);
			break;
		}
		mNowyear.setText(data+"");
	}
}